package cc.rengu.igas.channel.upac.core.service.trans;

import cc.rengu.igas.channel.upac.common.constant.UpacParamConstant;
import cc.rengu.igas.channel.upac.common.constant.UpacTreeNodeConstant;
import cc.rengu.igas.channel.upac.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.upac.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.upac.core.service.base.AuthService;
import cc.rengu.igas.channel.upac.facade.request.UnifiedOrgQueryRequest;
import cc.rengu.igas.channel.upac.facade.response.UnifiedOrgQueryResponse;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.DstChannelInfo;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;

/**
 * 统一机构查询.
 */
public class UnifiedOrgQueryService extends AuthService {


    @Override
    protected Object beforeBizProcess() throws Exception {
        UnifiedOrgQueryRequest unifiedOrgQueryRequest = new UnifiedOrgQueryRequest();
        ConvertUtil.convertOutput(unifiedOrgQueryRequest);
        return unifiedOrgQueryRequest;
    }

    @Override
    protected ChnlBizResponse callBizService(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        int iReturnCode = 0;
        String acctEncFlag = xmlTreeUtil.getXmlTreeStringValue(UpacTreeNodeConstant.ACCT_ENC_FLAG);
        ChnlBizResponse chnlBizResponse = new ChnlBizResponse();
        UnifiedOrgQueryResponse unifiedOrgQueryResponse = new UnifiedOrgQueryResponse();
        UnifiedOrgQueryRequest unifiedOrgQueryRequest = (UnifiedOrgQueryRequest) request;
        DstChannelInfo dstChannelInfo = (DstChannelInfo) xmlTreeUtil.getXmlTreeObjectValue(UpacTreeNodeConstant.DST_CHANNEL_INFO, DstChannelInfo.class);

        xmlTreeUtil.setXmlTreeStringValue(UpacTreeNodeConstant.txnType, "00");
        xmlTreeUtil.setXmlTreeStringValue(UpacTreeNodeConstant.txnSubType, "00");
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, unifiedOrgQueryRequest.getHeader().getTxnNum());
        iReturnCode = schedule.callService(dstChannelInfo.getDstServiceName(), 0);
        if (98 == iReturnCode) {
            rglog.error("调用银联全渠道服务<{}>超时，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            unifiedOrgQueryResponse.setRespCode(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
            unifiedOrgQueryResponse.setRespDesc(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespDesc());
            chnlBizResponse.setResult(unifiedOrgQueryResponse);
            return chnlBizResponse;
        } else if (0 != iReturnCode) {
            rglog.error("调用银联全渠道服务<{}>失败，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }
        ConvertUtil.convertOutput(unifiedOrgQueryResponse);
        unifiedOrgQueryResponse.setRespCode(convertRespCode(unifiedOrgQueryResponse.getRespCode()));
        chnlBizResponse.setResult(unifiedOrgQueryResponse);
        return chnlBizResponse;
    }

    @Override
    protected void afterBizProcess(ChnlBizResponse response) throws Exception {
        UnifiedOrgQueryResponse unifiedOrgQueryResponse = (UnifiedOrgQueryResponse) response.getResult();
        ConvertUtil.convertInput(unifiedOrgQueryResponse);
    }

    @Override
    protected String convertRespCode(String srcRespCode) {
        if (UpacParamConstant.SUCCESS_CODE.equals(srcRespCode)) {
            return OltpRpcdEnum.TRANS_SUCCESS.getRespCode();
        } else {
            return srcRespCode;
        }
    }
}
